package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.exception.ValidateException;
import com.cskaoyan.shopping.constants.ShoppingRetCode;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.service.IHomeService;
import net.bytebuddy.implementation.bytecode.Throw;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Aurhor: 徐云飞
 * @DATE: 2022/10/25 16:47
 * @Description:
 * @Version 1.0
 */
@Service
@Transactional
public class IHomeServiceImpl implements IHomeService {

    @Autowired
    ContentConverter contentConverter;
    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;

    @Override
    public Set<PanelDto> homepage() {


        Set<PanelDto> panelDtos = null;
        try {
            // List<Panel> panels = panelMapper.selectAll();
            Example example = new Example(Panel.class);
            example.createCriteria().andEqualTo("position", 0);
            List<Panel> panels = panelMapper.selectByExample(example);
            panelDtos = new HashSet();

            for (Panel panel : panels) {

                List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());
                panel.setPanelContentItems(panelContentItems);

                PanelDto panelDto = contentConverter.panel2Dto(panel);
                panelDtos.add(panelDto);

            }
        } catch (Exception e) {
            throw new ValidateException(ShoppingRetCode.PANEL_NOT_EXISTS.getCode(),
                    ShoppingRetCode.PANEL_NOT_EXISTS.getMessage());
        }

        return panelDtos;

    }
}
